/**
 *====================================================
 * 文件名称: FinanceSessionContext.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年9月23日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.context;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.project.entity.lexicon.OperatorRoleEnum;
import com.yuexunit.jsonutil.JSON;
import com.yuexunit.web.spi.SessionContext;

/**
 * @ClassName: FinanceSessionContext
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年9月23日 下午9:00:18
 */
public class FinanceSessionContext {

	static ThreadLocal<FinanceSessionAdditional> additional = new ThreadLocal<>();

	private static void initLocalAdditional() {
		if (additional.get() == null) {
			if (SessionContext.getExtra() == null) {
				throw new BusinessException("用户信息不存在,登陆接口请使用userNameLogin");
			}
			FinanceSessionAdditional bean = JSON.toBean(SessionContext.getExtra(), FinanceSessionAdditional.class);
			additional.set(bean);
		}
	}
	
	public static String getIsApp() {
		initLocalAdditional();
		return additional.get().getIsApp();
	}

	public static String getSessionUuid() {
		if (FinanceActionCommand.sessionUuid.get().equals(SessionContext.getSessionUuid())) {
			return SessionContext.getSessionUuid();
		}
		throw new BusinessException("上下文会话异常");
	}

	public static Long getAccountId() {
		return SessionContext.getAccountId();
	}

	public static String getAccountFullName() {
		initLocalAdditional();
		return additional.get().getFullName();
	}

	public static Long getAccountTenantId() {
		return SessionContext.getTenantId();
	}

	public static String getAppKey() {
		return SessionContext.getAppKey();
	}

	public static String getRegion() {
		initLocalAdditional();
		return additional.get().getRegion();
	}

	public static Long getEnterpriseId() {
		initLocalAdditional();
		Long enterpriseId = additional.get().getEnterpriseId();
		if (enterpriseId == null) {
			throw new BusinessException("该企业还未填写认证资料");
		}
		return enterpriseId;
	}
	
	public static Long getCompanyId() {
		initLocalAdditional();
		Long companyId = additional.get().getCompanyId();
		if (companyId == null) {
			throw new BusinessException("未找到登陆用户的物流企业");
		}
		return companyId;
	}

	public static String getEnterpriseName() {
		initLocalAdditional();
		return additional.get().getEnterpriseName();
	}

	public static Long getClerkId() {
		initLocalAdditional();
		Long clerkId = additional.get().getClerkId();
		if (clerkId == null) {
			throw new BusinessException("未找到登陆用户的物流人员信息");
		}
		return clerkId;
	}

	public static boolean isEnterprise() {
		OperatorRoleEnum operatorRole = OperatorRoleEnum.valueOf(FinanceConstant.APP_ROLE.get(FinanceSessionContext.getAppKey()));
		return OperatorRoleEnum.ENTERPRISE == operatorRole;
	}

	public static boolean isLogistics() {
		OperatorRoleEnum operatorRole = OperatorRoleEnum.valueOf(FinanceConstant.APP_ROLE.get(FinanceSessionContext.getAppKey()));
		return OperatorRoleEnum.LOGISTICS == operatorRole;
	}
}
